﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

namespace Elementum
{
    class Stage7 : Stage
    {
        public Stage7(Game game) : base(game) { }

        float GROUND_ALPHA = 0f;

        float timeTillPuff = 0.0f;
        SmokePlumeParticleSystemForStage6 smokePlume;
        const float TimeBetweenSmokePlumePuffs = 0.5f;
        
        public override void InitializeScenes()
        {
            base.InitializeScenes();
            for (int i = 1; i < 4; i++)
            {
                Animation a = new Animation("images/stages/stage3/city_night" + i);
                a.LayerDepth = 0.9f;
                _backGround.Add(a);
            }

            _backGroundPos = new Vector2(-435, -400);
            _scenceBound = new Rectangle(-50, -200,
               1580,
              650);
            camera.Bound = _scenceBound;
            camera.CameraTransfer = 100;
            // basePos = player.Position;

            for (int i = 0; i < 3; i++)
            {
                Animation wallBack = new Animation("images/stages/stage7/stage7_" + i);
                //wallBack.LayerDepth = 0.9f;
                _wallBackGround.Add(wallBack);
            }
            _wallBackGroungPos = new Vector2(-387, -275);

            smokePlume = new SmokePlumeParticleSystemForStage6(this, 3);

            Animation title = new Animation("images/title/stageTitle", new Rectangle(2, 284, 134, 42), new Point(134, 42));
            title.Scale = 1.2f;

            Transition tra;
            tra = new DefauldTransition(1, 0, 0, 60);
            tra.Register(this);
            tra = new TitleTransition(title, new Vector2(622, 135), 0, 1, 80, 220);
            tra.Register(this);

            GroundObject grount =
            StateFactory.CreateGround(this, 163, 152, 305, 20, 0);grount.Alpha = GROUND_ALPHA;
            MechanismGound mechanismGound = 
            StateFactory.CreateMechanismGound(this, 340, 152, 50, 20, 0, 0, 1000, 0, 1);
            
            Switch gameSwitch =
            StateFactory.CreateSwitch(this, 186, 117);gameSwitch.AddTarget(mechanismGound);
            grount =
            StateFactory.CreateGround(this, 490, 152, 250, 20, 0); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreateDoor("Stage7_Door_S1", this, 646, 192, "Stage7_Door_1");
            grount =
            StateFactory.CreateGround(this, 751, 152, 150, 20, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 98, 202, 146, 15, 0);grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 99, 280, 155, 20, 0); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreateKey("Stage7_Key_1", this, 106, 178, "Stage7_Door_1");
            GameDoor door =
            StateFactory.CreateDoor("Stage7_Door_S2", this, 222, 226, "Stage7_Door_2");//242, 245,
            door.Rotation = 1.57f;
            grount =
            StateFactory.CreateGround(this, 444, 280, 342, 20, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 444, 202, 343, 15, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 751, 202, 150, 15, 0); grount.Alpha = GROUND_ALPHA; 
            StateFactory.CreateKey("Stage7_Key_2", this, 729, 250, "Stage7_Door_2");
            StateFactory.CreateDoor("Stage7_Door_S3", this, 645, 320, "Stage7_Door_3");
            grount =
            StateFactory.CreateGround(this, 751, 280, 150, 20, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 128, 333, 223, 20, 0); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreateKey("Stage7_Key_3", this, 106, 306, "Stage7_Door_3");
            door =
            StateFactory.CreateDoor("Stage7_Door_S4", this, 291, 353, "Stage7_Door_4");//313, 373
            door.Rotation = 1.57f;
            grount =
            StateFactory.CreateGround(this, 479, 333, 272, 20, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 751, 333, 150, 20, 0); grount.Alpha = GROUND_ALPHA; grount =
            StateFactory.CreateGround(this, 573, 433, 1120, 20, 0f); grount.Alpha = GROUND_ALPHA;
            StateFactory.CreateKey("Stage7_Key_4", this, 848, 406, "Stage7_Door_4");

            StateFactory.CreatePortal("Stage7_Portal_1", this, 13, 36, "stage6", "Stage6_Portal_2");
            grount =
            StateFactory.CreateGround(this, -52, 96, 245, 20, 0);grount.Alpha = GROUND_ALPHA;

            grount =
            StateFactory.CreatekillBund(this, 871, 216, 233, 64, 1.57f); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreatekillBund(this, -32, 264, 324, 64, -1.57f); grount.Alpha = GROUND_ALPHA;
            //grount =
            //StateFactory.CreateGround(this, 1057, 431, 153, 15, 0); grount.Alpha = GROUND_ALPHA;
            mechanismGound = 
            StateFactory.CreateMechanismGound(this, 1183, 433, 100, 20, 0, 0, -150, 0, 30);
            
            gameSwitch =
            StateFactory.CreateSwitch(this, 1020, 398); gameSwitch.AddTarget(mechanismGound);
            grount =
            StateFactory.CreateGround(this, 1276, 281, 85, 15, 0); grount.Alpha = GROUND_ALPHA;
            
            mechanismGound = 
            StateFactory.CreateMechanismGound(this, 1283, 581, 100, 20, 0, 0, -148, 0, 50);
            
            SensorSwitch sensorSwitch =
            StateFactory.CreateSensorSwitch(this, 1275, 262);sensorSwitch.AddTarget(mechanismGound);
            grount =
            StateFactory.CreateGround(this, 1521, 433, 150, 20, 0);grount.Alpha = GROUND_ALPHA; 
            StateFactory.CreatePortal("Stage7_Portal_2", this, 1555, 373, "stage8", "Stage8Portal_1");
            grount =
            StateFactory.CreatekillBund(this, 1309, 645, 1024, 64, 0); grount.Alpha = GROUND_ALPHA;
            grount =
            StateFactory.CreateGround(this, -252, -2, 154, 207, 0f); grount.Alpha = GROUND_ALPHA; 
            grount =
            StateFactory.CreateGround(this, 1864, 371, 79, 248, 0f); grount.Alpha = GROUND_ALPHA;

            grount =StateFactory.CreateGround(this, 903, 221, 10, 250, 0);
            grount.Alpha = GROUND_ALPHA;
            grount = StateFactory.CreateGround(this, 866, 336, 65, 20, 0f);
            grount.Alpha = GROUND_ALPHA;
            grount = StateFactory.CreateGround(this, 874, 89, 68, 20, 0f);
            grount.Alpha = GROUND_ALPHA;
            Monster m = new Monster(this);
            m.SPEED = new Vector2(1.5f + (0.1f * 3), 0);
            m.Position = new Vector2(500, 115);//1150, 168
            actors.Add(m);

            _song = MusicManager.LoadSong("Music/stage6");
            if (GameClass._musicString == "Music/stage6")
            { }
            else
            {
                GameClass._musicString = "Music/stage6";
                MediaPlayer.Play(_song);
            }
            MediaPlayer.IsRepeating = true;
           // MediaPlayer.Volume = 1f;
           
            MusicManager.AddMusic("Music/doorlock", "doorlock", 0.001f, false);
            _bloom = _gameClass._BloomComponent;

            actors.Add(smokePlume);

            _startProtal = new ProtalCommand("stage7", "Stage7_Portal_1");

        }

        private void UpdateSmokePlume(float dt)
        {
            timeTillPuff -= dt;
            if (timeTillPuff < 0)
            {
                Vector2 where = Vector2.Zero;
                // add more particles at the bottom of the screen, halfway across.
                //this.Game.GraphicsDevice.Viewport;
                where.X = GameClass.RandomBetween(_scenceBound.Left, _scenceBound.Right);
                where.Y = GameClass.RandomBetween(_scenceBound.Top, _scenceBound.Bottom);
                smokePlume.AddParticles(where);

                // and then reset the timer.
                timeTillPuff = TimeBetweenSmokePlumePuffs;
            }
        }

        public override void Restart()
        {
            //base.Restart();
            StageManager.Save.StageStateTable.Remove(StageManager.CurrentStateID);
            StageManager.ProtalCommand = new ProtalCommand("stage7", "Stage7_Portal_1");
        }

        public override void OnDraw(SpriteBatch spriteBatch, int delta)
        {

            base.OnDraw(spriteBatch, delta);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transform);
            // basePos = player.Position;

            foreach (VisibleObject b in actors)
                b.OnDraw(spriteBatch, camera.Transform);

            spriteBatch.End();
            
            //spriteBatch.Begin();
            //spriteBatch.DrawString(Font, "Counter : " + scriptCounter, new Vector2(50, 50), Color.White);
            //spriteBatch.DrawString(Font, "temp" + mechanismGound.tempCount + "count"+mechanismGound.PositiveAction.Count, new Vector2(50, 50), Color.Black);
            //spriteBatch.End();

        }

        public override void Update(int delta)
        {
            base.Update(delta);
            //camera.Update(delta);
            UpdateSmokePlume(0.01f);
            //a.Update(delta);
            //  MusicManager.FadeInMusic("first");
            //MusicManager.FadeOutMusic("first");



        }

        public override Stage Clone()
        {
            Stage7 stage = new Stage7(Game);
            stage.InitializeScenes();
            return stage;
        }
    }
}
